package com.fzb.forum4j.po;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Id;

import org.apache.struts2.json.JSONUtil;

public class QueryMode<T>{

	private T t;
	private long nd;
	private String sord;
	private Boolean _search;
	private Map<String,Object> data=new HashMap<String, Object>();
	private int rows;
	private int page;
	private int id;
	private String oper;
	private String keyword;
	private String ids;
	private String filters;
	private Map<String,Object> filtersJson;
	public String getOper() {
		return oper;
	}
	public void setOper(String oper) {
		this.oper = oper;
	}
	private String sidx;
	public int getStartRecord(){
		if(((Integer)data.get("total")!=null)){
			if(((Integer)data.get("total"))<page) setPage(((Integer)data.get("total")));
		}
		return (page-1)*rows;
	}
	public int getRows() {
		if(rows<=0) rows=10;
		return rows;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	
	public int getPage() {
		if(page<=0) page=1;
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public Map<String,Object> getData() {
		return data;
	}
	public void setData(Map<String,Object> data) {
		this.data = data;
	}
	public T getT() {
		return t;
	}
	public void setT(T t) {
		this.t = t;
	}
	public long getNd() {
		return nd;
	}
	public void setNd(long nd) {
		this.nd = nd;
	}
	public String getSord() {
		if(sord==null) sord="desc";
		return sord;
	}
	public void setSord(String sord) {
		this.sord = sord;
	}
	public Boolean get_search() {
		return _search;
	}
	public void set_search(Boolean _search) {
		this._search = _search;
	}
	public String getSidx() {
		return sidx;
	}
	public void setSidx(String sidx) {
		this.sidx = sidx;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
		setEid();
	}
	public String getKeyword() {
		return keyword;
	}
	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}
	public void setEid(){
		String eid=getEidAndPackage(t.getClass().getDeclaredFields(), "");
		if(eid.length()==0){
			eid=getEidAndPackage(t.getClass().getSuperclass().getDeclaredFields(), "");
		}
		try {
			PropertyDescriptor pd = new PropertyDescriptor(eid, t.getClass());
			Method m = pd.getWriteMethod();
			m.invoke(t, id);
		} catch (IllegalAccessException e1) {
			e1.printStackTrace();
		} catch (IllegalArgumentException e1) {
			e1.printStackTrace();
		} catch (InvocationTargetException e1) {
			e1.printStackTrace();
		} catch (IntrospectionException e) {
			e.printStackTrace();
		}  catch (SecurityException e) {
			e.printStackTrace();
		}
		
	}
	private String getEidAndPackage(Field[] f,String eidName) {
		for (Field field : f) {
			if(field.getAnnotation(Id.class)!=null){
				eidName=field.getName();
				break;
			}
		}
		return eidName;
		
	}
	public String getIds() {
		return ids;
	}
	public void setIds(String ids) {
		this.ids = ids;
	}
	public String getFilters() {
		return filters;
	}
	public void setFilters(String filters) {
		try {
			this.filtersJson=(Map<String, Object>) JSONUtil.deserialize(filters);
			List<Map<String,Object>> rules=(List<Map<String, Object>>) filtersJson.get("rules");
			filters="1=1 ";
			if(filtersJson.get("groupOp").equals("AND")){
				for (Map<String, Object> map : rules) {
					if(map.get("field").equals("id")){
						String eid=getEidAndPackage(t.getClass().getDeclaredFields(), "");
						if(eid.length()==0){
							eid=getEidAndPackage(t.getClass().getSuperclass().getDeclaredFields(), "");
						}
						filters+="and "+eid+getOpByStr(map.get("op"))+map.get("data");
					}
					else{
						filters+="and "+map.get("field")+""+map.get("op")+map.get("data");;
					}
				}
			}
		} catch (org.apache.struts2.json.JSONException e) {
			e.printStackTrace();
		}
		this.filters = filters;
	}
	
	private String getOpByStr(Object op){
		String rop=null;
		if(op.equals("eq")){
			rop="=";
		}
		else if(op.equals("ni")){
			rop="!=";
		}
		else if(op.equals("in")){
			rop="in";
		}
		else if(op.equals("ne")){
			rop="!=";
		}
		else if(op.equals("cn")){
			rop="!=";
		}
		else if(op.equals("ni")){
			rop="!=";
		}
		else if(op.equals("ni")){
			rop="!=";
		}
		return rop;
	}
 
}
